/**
 * @Date 2025/1/8 16:51
 * @Created by 衡盛永
 */

package org.example.deploy.controller;

//import io.minio.MinioClient;
//import io.minio.PutObjectArgs;

import org.example.deploy.config.MinioConfig;
import org.example.deploy.dto.ResultDto;
import org.example.deploy.vo.file.FileVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;

@RestController
public class FileController {

    // 注入minio配置文件
//    @Autowired
//    private MinioConfig minioConfig;

    // 注入minio client
//    @Autowired
//    private MinioClient minioClient;

//    @PostMapping("/upload")
//    public String upload(@RequestParam("file") MultipartFile file) {
//        if (file.isEmpty()) {
//            return "Please select a file to upload.";
//        }
//        try {
//            System.out.println("file name:" + file.getOriginalFilename());
//            byte[] bytes = file.getBytes();
//            String uploadDir = System.getProperty("user.dir") + "/src/main/resources/upload/";
//            File uploadedFile = new File(uploadDir + file.getOriginalFilename());
//            file.transferTo(uploadedFile);
//            return "File uploaded successfully!";
//        } catch (IOException e) {
//            e.printStackTrace();
//            return "File upload failed!";
//        }
//    }
//
//    @PostMapping("/minioUpload")
//    public ResultDto minioUpload(@RequestParam("file") MultipartFile file) {
//        FileVo fileVo = new FileVo();
//        try {
//            // 获取文件真实名称
//            String originalFilename = file.getOriginalFilename();
//            // 获取文件的扩展名 例如.jpg .doc
//            String extname = originalFilename.substring(originalFilename.lastIndexOf("."));
//            // 构建文件上传相关信息
//            PutObjectArgs args = PutObjectArgs.builder()
//                    .bucket(minioConfig.getBucketName())
//                    .object(originalFilename)
//                    .stream(file.getInputStream(), file.getSize(), -1)
//                    .contentType(file.getContentType())
//                    .build();
//            // 将文件上传到minio服务器
//            minioClient.putObject(args);
//            // 组装文件信息，返回前端 或者存入数据路
//            String url = minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + originalFilename;
//            fileVo.setUrl(url);
//            fileVo.setSize(file.getSize());
//            fileVo.setFileName(originalFilename);
//            fileVo.setExtname(extname);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//        return ResultDto.success("上传成功", fileVo);
//    }
//
//    @PostMapping("/download")
//    public String download() {
//        return "下载成功!";
//    }
}
